<?php
class ContractPdfUtil{
	public static function makeContractPdf($contractid, $mail = false, $invoicepdf = null, $salesorderpdf = null, $quotepdf = null,$outputFolder = 'pdf',$saveToFile = false)
	{
		$contract = Contracts::model()->findByPk($contractid);


		$location = Locations::model()->find(array(
			'condition' => 'locno=:locno and custno=:custno',
			'params' => array(
				'locno' => $contract->locno,
				'custno' => $contract->custno
			)));
		$customer = Customers::model()->findByPk($contract->custno);

		$pdf = new ContractPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
		$pdf->contractno = $contractid;
		$pdf->custno = $contract->custno;
		$pdf->date = $contract->contractstartdate;
		// set document information
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetAuthor('QSI');
		$pdf->SetTitle('Contract #' . $contractid);
		$pdf->SetSubject('Contract');
		$pdf->SetKeywords('');



		$pdf->setPrintHeader(false);
		// set header and footer fonts
		$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
		$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

		// set default monospaced font
		$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

		//set margins
		$pdf->SetMargins(PDF_MARGIN_LEFT, 0, PDF_MARGIN_RIGHT);
		$pdf->SetHeaderMargin(0);
		$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

		//set auto page breaks
		$pdf->SetAutoPageBreak(TRUE, 10);

		//set image scale factor
		$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

		//set some language-dependent strings
		// $pdf->setLanguageArray($l);
		// ---------------------------------------------------------
		// set font
		$pdf->SetFont('dejavusans', '', 10);


		$pdf->AddPage();
		// $pdf->writeHTMLCell(87.8, 20, 80, 5, $html = '<small><i>This is not an invoice.</i></small>');
		$pdf->RoundedRect(17, 16.3, 87, 26, 6.50, '1111');
		$pdf->Image(K_PATH_IMAGES . 'QSI1.png', 23, 17, 25, 24);
		$pdf->MultiCell(40, 4, 'QSI, INC', false, 'C', false, 1, 65, 17);
		$pdf->MultiCell(40, 4, '107 Schuler Dr.', false, 'C', false, 1, 63, 22);
		$pdf->MultiCell(40, 4, 'Bardstown KY 40004', false, 'C', false, 1, 63, 27);
		$pdf->MultiCell(60, 4, 'Phone: 502-350-1000 ', false, 'C', false, 1, 53, 32);
		$pdf->MultiCell(40, 4, 'Fax: 502-350-1002 ', false, 'C', false, 1, 63, 37);
		$pdf->RoundedRect(106.1, 16.3, 87.8, 13, 1.50, '1111', 'DF', array(), array(0, 255, 255));
		$pdf->writeHTMLCell(87.8, 20, 106.1, 20, $html = '<h1 style="text-align: center">CONTRACT</h1>');
		$html = '                <br/><br/><br/><br/>
<table border="0" cellspacing="10" cellpadding="10"  >
     <tr>

        <th style="width:52%;" align="right" colspan = "2" >

        </th>
        <th  colspan = "2">

              <table cellspacing="1" cellpadding="2" >
                <tr>
                <h2></h2> <br><br>
                    <td align="center" style="background-color: #dddddd;border: 1px solid black;" >Date</td>

                    <td  align="center" style="background-color: #dddddd;border: 1px solid black;"> Contract No.</td>
                    <td  align="center" style="background-color: #dddddd;border: 1px solid black;">Cust No.</td>

                </tr>

                <tr>
                <td align="center" style="border: 1px double black;" ><b>' . $pdf->date . '</b></td>
                    <td align="center" style="border: 1px double black;" ><b>' . $pdf->contractno . '</b></td>

                    <td align="center" style="border: 1px double black;" ><b>' . $pdf->custno . '</b></td>
                </tr>
                  </table>

        </th>
    </tr>
    </table>
                ';

		$pdf->writeHTMLCell(200.5, 60, 0, 0, $html);
		$itemRows = '';
		$subtotal = 0;
		$itemCounter = 0;
		/*$locationContacts = $location->getLocationcontacts();

		foreach ($locationContacts as $item)
		{

			$itemRows .= '<tr>
                         <td  align="left"  >' . $item->contact . '</td>
                         <td  align="left"  >' . $item->email . '</td>
                         <td  align="left"  >' . $item->phoneoffice . '</td>

                         <td  align="left"  >' . $item->jobtitle . '</td>
                        </tr>';
			$itemCounter++;
		}*/

		$planList = Equipment::model()->getPlanTypeList();
		$eqRows = '';
		$amount = 0;
		$eqSchedule = array();



		$html = '
    <table border="0" cellspacing="5" cellpadding="5">
     <tr>
        <td style="background-color: #dddddd;border: 1px double black;" colspan="2">BILL TO
</td>
        <td style="background-color: #dddddd;border: 1px double black;" colspan="2">SERVICE LOCATION</td>
    </tr>
    </table>';


		//writeHTMLCell ($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true)
		//  $pdf->writeHTML($html, true, false, true, false, '');
		$pdf->writeHTMLCell(181, 0, 15, 45, $html);
		$pdf->writeHTMLCell(150, 0, 16, 58, "&nbsp;<b>$customer->lastname</b>");
		$pdf->writeHTMLCell(150, 0, 17, 62, "<b>$customer->add1</b>");
		if ($customer->add2)
		{
			$pdf->writeHTMLCell(150, 0, 17, 66, "<b>$customer->add2</b>");
			$pdf->writeHTMLCell(150, 0, 17, 70, "<b>$customer->city $customer->state $customer->zip </b>");
		}
		else
		{
			$pdf->writeHTMLCell(150, 0, 17, 66, "<b>$customer->city $customer->state $customer->zip </b>");
		}

		$pdf->writeHTMLCell(150, 0, 106, 58, "<b>$location->locname</b>");
		$pdf->writeHTMLCell(150, 0, 106, 62, "<b>$location->add1</b>");
		if ($location->add2)
		{
			$pdf->writeHTMLCell(150, 0, 106, 66, "<b>$location->add2</b>");
			$pdf->writeHTMLCell(150, 0, 106, 70, "<b>$location->city $location->state $location->zip</b>");
		}
		else
		{
			$pdf->writeHTMLCell(150, 0, 106, 66, "<b>$location->city $location->state $location->zip</b>");
		}

		$termsList = Customers::model()->getTermList();
		$typeList = ContractType::getListData();
		$billList = array(0 => 'Yearly', 1 => 'Monthly', 2 => 'Quarterly');
		$startdate = explode('-', $contract->contractstartdate);
		$enddate = explode('-', $contract->contractenddate);
		$html = '  <br> <br> <br><br>

<table  cellspacing="0" cellpadding="1" border="1">
    <tr>
    <th align="center" style="background-color: #dddddd;border: 1px double black;">CONTRACT TYPE</th>
     <th align="center" style="background-color: #dddddd;border: 1px double black;">START</th>
     <th align="center" style="background-color: #dddddd;border: 1px double black;">END/RENEW</th>
     <th align="center" style="background-color: #dddddd;border: 1px double black;">BILL TYPE</th>
     <th  align="center" style="background-color: #dddddd;border: 1px double black;">TERMS</th>
     <th  align="center" style="background-color: #dddddd;border: 1px double black;">SALES PERSON</th>
    </tr>
    <tr>
         <td align="center"  >' . $typeList[$contract->contractype] . '</td>
        <td align="center"  >' . $startdate[1] . '/' . $startdate[2] . '/' . $startdate[0] . '</td>
        <td align="center"  >' . $enddate[1] . '/' . $enddate[2] . '/' . $enddate[0] . '</td>
         <td align="center"  >' . $billList[$contract->billtype] . '</td>
        <td align="center"  >' . $termsList[$customer->terms] . ' </td>
        <td align="center"  >' . Employees::getEmployeeName($contract->salesperson) . '</td>

    </tr>
</table>';
		$pdf->writeHTML($html, true, false, true, false, '');/*
		if ($contract->contractype != 4)
		{
			$html='

         <h2>Location Contacts</h2>
<table cellspacing="0" cellpadding="3" style="border: 1px solid black;">
<tr>
<th  align="left" style="width:75px;background-color: #dddddd;border: 1px double black;">Name</th>
<th  align="left" style="width:360px;background-color: #dddddd;border: 1px double black;">Email</th>
<th  align="left" style="width:100px;background-color: #dddddd;border: 1px double black;">Phone Office </th>
<th  align="left" style="width:100px;background-color: #dddddd;border: 1px double black;">Job Title  </th>
</tr>' . "

$itemRows
</table>
<br>
";
			$pdf->writeHTML($html, true, false, true, false, '');
		}*/

		$html = '';
		$headerFlag = false;
		$equipmentList = ContractEquipment::model()->findAll("contractid='$contractid'");

		if ($contract->contractype == 4)
		{
			$equipHeader = '<table  cellspacing="0" cellpadding="3"  style="border: 1px solid black;">
                <tr>
                <th align="center" style="width:100px;background-color: #dddddd;border: 1px double black;">Equip #</th>
                <th align="left"  style="width:325px;background-color: #dddddd;border: 1px double black;">Description </th>
                <th align="center" style="width:60px;background-color: #dddddd;border: 1px double black;">Serial No</th>
                <th align="center" style="width:60px;background-color: #dddddd;border: 1px double black;">Manf.</th>
                <th align="center" style="width:75px;background-color: #dddddd;border: 1px double black;">Model</th>                
                </tr>';
		}
		else
		{
			$equipHeader = '<table  cellspacing="0" cellpadding="3"  style="border: 1px solid black;">
                <tr>
                <th align="center" style="width:100px;background-color: #dddddd;border: 1px double black;">QSI Equip. No</th>
                <th align="center" style="width:60px;background-color: #dddddd;border: 1px double black;">QTY</th>
                <th align="left"  style="width:175px;background-color: #dddddd;border: 1px double black;">Description </th>
                <th align="center" style="width:75px;background-color: #dddddd;border: 1px double black;">Plan </th>
                <th align="right" style="width:75px;background-color: #dddddd;border: 1px double black;">Price </th>
				<th align="right" style="width:75px;background-color: #dddddd;border: 1px double black;">Pro-rate </th>
				<th align="right" style="width:75px;background-color: #dddddd;border: 1px double black;">Sub total </th>
                </tr>';
		}
		foreach ($equipmentList as $item)
		{
			if ($item->planid == 8)
			{
				$eqSchedule[] = $item->equipmentid;
			}
			$original = Equipment::model()->findByPK($item->equipmentid);
			$mfg = '';
			$modelNumber = '';
			$serial = '';
			$description = '';
			if ($contract->contractype == 4)
			{
				if ($original != null)
				{
					$description = $original->description;
					$mfg = $original->mfg;
					$modelNumber = $original->model;
					$serial = $original->serial;
				}


				$eqRows .= '<tr>
                         <td align="center"  >' . $item->equipmentid . '</td>
                         <td align="center"  >' . $description . '</td>
                         <td align="left"  >' . $serial . '</td>
                         <td align="center"  >' . $mfg . '</td>
                         <td align="right"  >' . $modelNumber . '</td>
                        </tr>';
			}
			else
			{
				if ($original != null)
				{
					$description = $original->description;
				}

				if (isset($planList[$item->planid]))
				{
					$plan = $planList[$item->planid];
				}
				else
				{
					$plan = 'Not Set';
				}
				if ($item->sl)
				{
					$plan = FlOptions::model()->getText($item->sl) . ' & ' . $plan;
				}
				$eqRows .= '<tr>
                         <td align="center"  >' . $item->equipmentid . '</td>
                         <td align="center"  >' . $item->qty . '</td>
                         <td align="left"  >' . $description . '</td>
                         <td align="center"  >' . $plan . '</td>
                                <td align="right"  >' . $item->price . '</td>
                          <td align="right"  >' . number_format($item->prorate,2) . '</td>

                          <td align="right"  >' . number_format($item->price + $item->prorate,2) . '</td>
                        </tr>';
			}

			$itemCounter++; 
			$amount += ($item->price + $item->prorate) * $item->qty;
		}


		//$pdf->AddPage();
		//$pdf->MultiCell(0, 4, 'QSI, INC - Contract No. ' . $contractid, false, 'C', false, 1, 32, 17);

		if ($eqRows)
		{
			$html = '<br><br><h2>Equipment</h2>' . $equipHeader .
					$eqRows .
					"</table>" ;
			if ($contract->contractype != 4)
			{
				$note = '';
				if($contract->billtype == 0 ){
					$note = 'Note:  This amount does not include taxes.';
				} else {
					$note = 'Note:  This contract is on billing cycle.  You will receive a contract statement each time your invoice are billed.  ';
				}

				$html.='<table>
                                <tr>
                                    <td style="width:630px;" align="right" >
                                    <br><br><b>Contract Amount</b><br>' .
						number_format($amount, 2) .
						'</td></tr><tr><td>
						'.$note.'
						</td></tr>
						</table>';
			}


			$pdf->writeHTML($html, true, false, true, false, '');
		}
		$invoiceHeader = '<table  cellspacing="0" cellpadding="3"  style="border: 1px solid black;">
                <tr>
                <th align="center" style="width:100px;background-color: #dddddd;border: 1px double black;">DATE</th>
                <th align="center" style="width:100px;background-color: #dddddd;border: 1px double black;">INVOICE</th>
                <th align="left"  style="width:100px;background-color: #dddddd;border: 1px double black;"> SUB-TOTAL</th>
                <th align="center" style="width:100px;background-color: #dddddd;border: 1px double black;">TAX </th>
                <th align="right" style="width:100px;background-color: #dddddd;border: 1px double black;">TOTAL </th>
                </tr>';

		$invoiceRows ='';
		foreach($contract->invoiceList as $invoice){
			if(!$invoice->creditMemoList)
			$invoiceRows .= '<tr>
                         <td align="center"  >' . $invoice->invoicedate . '</td>
                         <td align="center"  >' . $invoice->invoice . '</td>
                         <td align="right"  >' . number_format($invoice-> getSubTotal(),2) . '</td>
                         <td align="right"  >' . number_format($invoice->getTotalTax(),2) . '</td>

                          <td align="right"  >' . number_format($invoice->getTotal(),2) . '</td>
                        </tr>';
		}
		if ($invoiceRows)
		{
			$html = '<br><br><h2>Invoice List</h2>' . $invoiceHeader .
					$invoiceRows .
					"</table>" ;
			$pdf->writeHTML($html, true, false, true, false, '');
		}
		else {
			$html = '<h4>Note:  Invoice has not been created for this contract</h4>';
			$pdf->writeHTML($html, true, false, true, false, '');
		}
		$html = '';
		if ($contract->contractype == 4)
		{
			$pdf->writeHTML($html, true, false, true, false, '');
			$html = ' <h2>Software Options</h2>
                <table  width="100%" cellspacing="0" cellpadding="3"  style="border: 1px solid black;">
                <tr>
                <th align="center" style="width:200px;background-color: #dddddd;border: 1px double black;">Software Platform</th>                
                <th align="center" style="width:215px;background-color: #dddddd;border: 1px double black;">Software Prod. Id1</th>
                <th align="center" style="width:215px;background-color: #dddddd;border: 1px double black;">Software Prod. Id 2</th>                

                </tr>';
			$contractSoftwareLedgerList = ContractSofwareMaintLedger::model()->findAll('contractid=:contractid', array(
				'contractid' => $contractid
			));
			foreach ($contractSoftwareLedgerList as $contractSoftwareLedger)
			{
				$html.='<tr>
                         <td align="center"  >' . $contractSoftwareLedger->software_platform . '</td>
                         <td align="center"  >' . $contractSoftwareLedger->software_product_id_1 . '</td>                     
                         <td align="center"  >' . $contractSoftwareLedger->software_id_2 . '</td>                     
                        </tr>';
			}
			$html.='</table>';
			$pdf->writeHTML($html, true, false, true, false, '');
		}
		if ($contract->discounted)
		{
			$html = 'Any discount will appear on your invoice.  Thank you.';
			$pdf->writeHTMLCell(0, 0, 60, 276, $html);
		}
		if (!empty($eqSchedule))
		{
			$pdf->AddPage();
			$pdf->MultiCell(0, 4, 'QSI, INC - Contract No. ' . $contractid, false, 'C', false, 1, 32, 17);
			$html = Yii::app()->controller->renderPartial(
				'/pdf/eqsch',
				array(
					'eqSchedule' => $eqSchedule,
					'contract'=>$contract
				),
				true
			);
			//echo $html;
			//return;
			$pdf->writeHTML($html, true, false, true, false, '');
		}
		if ($contract->contractype != 4 && isset($_POST['planpdf']))
		{
			$pdf->AddPage();
			$pdf->MultiCell(0, 4, 'QSI, INC - Contract No. ' . $contractid, false, 'C', false, 1, 32, 17);


			$planTypes = Plantypes::model()->findAll();
			$html = Yii::app()->controller->renderPartial('/pdf/plans', array('planTypes' => $planTypes), true);
			$pdf->writeHTML($html, true, false, true, false, '');
			$html = 'NOTE:
All service performed to repair equipment due to vandalism,
 acts of God, customer misuse or abuse will be billed at our
 normal hourly rates.';
			$pdf->writeHTMLCell(0, 0, 20, 276, $html);
			// $pdf->AddPage();
			// $pdf->MultiCell(0, 4, 'QSI, INC - Contract No. ' . $contractid, false, 'C', false, 1, 32, 17);
			// $html = Yii::app()->controller->renderPartial('/pdf/terms', array(), true);

			// $pdf->writeHTML($html, true, false, true, false, '');
			$pdf->MultiCell(0, 4, 'QSI, INC - Contract No. ' . $contractid, false, 'C', false, 1, 32, 17);
		}
		// set color for background
		$pdf->SetFillColor(255, 255, 215);

		// set font
		$pdf->SetFont('helvetica', '', 8);


		// reset pointer to the last page
		//$pdf->lastPage();

		//echo $html;
		// reset pointer to the last page
		$pdf->lastPage();


		// ---------------------------------------------------------
		if ($mail)
		{

			$body = $_POST['message'];
			$address = $_POST['email'];
			$emailArray = explode(',', $address);


			$subject = $_POST['subject'];
			$pdfName = self::outputContractPdf($pdf, $contract, true,$outputFolder,$saveToFile);
			$files[] = $pdfName;



			if (isset($_POST['quotepdf']))
			{
				$msg='';
				/* @var $contract Contracts */
				$quoteList = $contract->quoteList;
				if (count($quoteList) == 0)
				{
					$msg .='There is no quote for this contract';
				}
				else
				{
					foreach ($quoteList as $invoice)
					{
						/* @var $quote Invoice */
						$pdf = InvoicePdfUtil::getInvoicePdf($invoice->invoice);
						if ($pdf != NULL)
						{
							$locationText = StringUtil::removeSpecialCharacter($invoice->location->locname).'_'.StringUtil::removeSpecialCharacter($invoice->location->add1);
							$quoteFile = 'pdf/'.$locationText.'_'.StringUtil::removeSpecialCharacter($invoice->invoicetype).'_'. $invoice->invoice . '.pdf';
							$pdf->Output($quoteFile, 'F');
							$files[] = $quoteFile;
							$log = new PrintLog();
							$log->date = date('Y-m-j');
							$log->time = date('h-i-s');
							$log->type = $invoice->invoicetype . ' Report';
							$log->number = $invoice->invoice;
							$log->emailto = $_POST['email'];
							$log->print = 'Yes';
							$log->save();
						}
					}
				}
			}
			if (isset($_POST['salesorderpdf']))
			{

				$salesOrderList = $contract->salesOrderList;
				if (count($salesOrderList) > 0)
				{
					foreach ($salesOrderList as $invoice)
					{
						/* @var $quote Invoice */
						$pdf = InvoicePdfUtil::getInvoicePdf($invoice->invoice);
						if ($pdf != NULL)
						{
							$locationText = StringUtil::removeSpecialCharacter($invoice->location->locname).'_'.StringUtil::removeSpecialCharacter($invoice->location->add1);
							$saFile = 'pdf/'.$locationText.'_'.StringUtil::removeSpecialCharacter($invoice->invoicetype).'_' . $invoice->invoice . '.pdf';
							$pdf->Output($saFile, 'F');
							$files[] = $saFile;

							$log = new PrintLog();
							$log->date = date('Y-m-j');
							$log->time = date('h-i-s');
							$log->type = $invoice->invoicetype . ' Report';
							$log->number = $invoice->invoice;
							$log->emailto = $_POST['email'];
							$log->print = 'Yes';
							$log->save();
						}
					}
				}
				else
				{
					$msg .='There is no sales order for this contract';
				}
			}
			if (isset($_POST['invoicepdf']))
			{

				$invoiceList = $contract->getInvoiceList();
				if (count($invoiceList) > 0)
				{
					foreach ($invoiceList as $invoice)
					{
						/* @var $quote Invoice */
						$pdf = InvoicePdfUtil::getInvoicePdf($invoice->invoice);
						if ($pdf != NULL)
						{
							$locationText = StringUtil::removeSpecialCharacter($invoice->location->locname).'_'.StringUtil::removeSpecialCharacter($invoice->location->add1);
							$invoiceFile = 'pdf/'.$locationText.'_'.StringUtil::removeSpecialCharacter($invoice->invoicetype).'_' . $invoice->invoice . '.pdf';
							$pdf->Output($invoiceFile, 'F');
							$files[] = $invoiceFile;
							$log = new PrintLog();
							$log->date = date('Y-m-j');
							$log->time = date('h-i-s');
							$log->type = $invoice->invoicetype . ' Report';
							$log->number = $invoice->invoice;
							$log->emailto = $_POST['email'];
							$log->print = 'Yes';
							$log->save();
						}
					}
				}
				else
				{
					$msg .='There is no invoice for this contract';
				}
			}
			if ( ! EmailUtil::sendEmail($subject,$emailArray,$body,$files))
			{

				$result = array('success' => false, 'msg' => 'Cant not send email');
				echo json_encode($result);
			}
			else
			{
				$log = new PrintLog();
				$log->date = date('Y-m-j');
				$log->time = date('h-i-s');
				$log->type = 'Contract Report';
				$log->number = $contractid;
				$log->emailto = $_POST['email'];
				$log->print = 'Yes';
				$log->save();
				$result = array('success' => true);
				echo json_encode($result);
			}
		}
		else
		{
			//Close and output PDF document
			self::outputContractPdf($pdf, $contract,false,$outputFolder,$saveToFile);


			//  $pdf->Output('Contract-' . time(), 'I');

			$log = new PrintLog();
			$log->date = date('Y-m-j');
			$log->time = date('h-i-s');
			$log->type = 'Contract Report';
			$log->number = $contractid;
			$log->emailto = 'noemail';
			$log->print = 'Yes';
			$log->save();
		}

		//============================================================+
		// END OF FILE
		//============================================================+
		//

	}


	//there are 3 mode: I, F, M
	protected static function outputContractPdf(ContractPDF &$pdf, $contract, $email = false,$outputFolder='pdf',$saveToFile = false)
	{
		$locationText = StringUtil::removeSpecialCharacter($contract->location->locname).'_'.StringUtil::removeSpecialCharacter($contract->location->add1);
		if(trim($outputFolder)!=''){
			$pdfName = $outputFolder.DIRECTORY_SEPARATOR .$locationText.'_Contract-' . $contract->contractid . '.pdf';
			$emailName = $outputFolder.DIRECTORY_SEPARATOR .$locationText.'_Contract-'. $contract->contractid . '_email'.'.pdf';
		}else{
			$pdfName = $locationText.'_Contract-' . $contract->contractid . '.pdf';
			$emailName = $locationText.'_Contract-'. $contract->contractid . '_email'.'.pdf';
		}


		if ($contract->contractype == 4)
		{
			$pdf->Output($pdfName, 'F');
			if (file_exists($pdfName) && file_exists('letter1.pdf'))
			{
				Yii::import('ext.PDFMerger.*');
				$merger = new PDFMerger();
				$merger->addPDF($pdfName);
				$merger->addPDF('letter1.pdf');
				if($email){

					$merger->merge('file',$emailName);
					return $emailName;
				}else{
					$merger->merge();
				}

			}
		}
		else
		{
			if($email){
				$pdf->Output($emailName, 'F');
				return $emailName;
			}else{
				if($saveToFile){
					$pdf->Output($emailName, 'F');
				}else{
					$pdf->Output($pdfName, 'I');
				}

			}

		}


	}
}

	
